package com.fpx.order.plat.bussiness.plat.base;

import java.io.IOException;

import org.apache.http.HttpException;
import org.apache.log4j.Logger;

import com.amazonservices.mws.orders.MarketplaceWebServiceOrdersException;
import com.ebay.sdk.ApiException;
import com.ebay.sdk.SdkException;
import com.fpx.api.bean.Exception.CheckDataException;
import com.fpx.api.bean.request.GetOrderRequest;
import com.fpx.api.bean.response.GetOrderResponse;
import com.fpx.api.service.IAmazonService;
import com.fpx.api.service.IEbayService;
import com.fpx.api.service.ISmtService;
import com.fpx.api.util.Constant;
import com.fpx.order.plat.bussiness.system.PlatConstants;

/**
 * ebay 请求拉单服务
 * 
 * @createtime 2015年1月10日 上午9:21:43
 * @author jonsen
 */
public class SdkCommonRequestOrderThread implements Runnable {
	
	private final static Logger LOGGER = Logger.getLogger(SdkCommonRequestOrderThread.class);
	/**
	 * 订单请求对象
	 */
	private GetOrderRequest request;
	/**
	 * 订单响应对象
	 */
	private GetOrderResponse response;

	private IEbayService ebayService;

	private IAmazonService amazonService;

	private ISmtService smtService;

	private boolean flag = false;

	/** 渠道 */
	private String platformCode;

	public SdkCommonRequestOrderThread() {

	}

	public SdkCommonRequestOrderThread(GetOrderRequest request,
			String platformCode, Object obj) {
		this.request = request;
		this.platformCode = platformCode;
		if (obj instanceof IEbayService) {
			this.ebayService = (IEbayService) obj;
		} else if (obj instanceof IAmazonService) {
			this.amazonService = (IAmazonService) obj;
		} else if (obj instanceof ISmtService) {
			this.smtService = (ISmtService) obj;
		}
	}

	@Override
	public void run() {
		if (platformCode.equals(PlatConstants.ORDER_SOURCE_EBAY)) {
			ebayRecursive(3);// 如果是网络超时就请求 最大次数为3
		} else if (platformCode.equals(PlatConstants.ORDER_SOURCE_AM)) {
			amazonRecursive(3);// 如果是网络超时就请求 最大次数为3
		} else if (platformCode.equals(PlatConstants.ORDER_SOURCE_SMT)) {
			smtRecursive(3);
		}
	}

	public void ebayRecursive(int a) {
		try {
			if (a > 0) {
				response = ebayService.getOrder(request);
			}
			// 只有请求成功或请求次数上限才为true
			flag = true;
		} catch (ApiException e) {
			LOGGER.error("【ebay】--调用获取订单业务类时发生Api异常---异常信息：\n"+e.getMessage());
			response = new GetOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());
			ebayRecursive(0);//
		} catch (SdkException e) {
			
			response = new GetOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());
			System.out.println(e.getMessage());
			if (e.getMessage().contains("Validation of the authentication")) {
				a = 0;// 请求授权异常 不用再次请求
			}
			--a;
			if(a==0){//最后一次调用失败后记录日志 
				LOGGER.error("【ebay】--调用获取订单业务类时发生Sdk异常---异常信息：\n"+e.getMessage());
			}
			ebayRecursive(a);// 如果是网络异常就请求再次请求
		} catch (CheckDataException e) {
			LOGGER.error("【ebay】--调用获取订单业务类时发生CheckData异常---异常信息：\n"+e.getMessage());
			response = new GetOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());
			ebayRecursive(0);// request请求参数 不用再次请求
		} catch (Exception e) {
			LOGGER.error("【ebay】--调用获取订单业务类时发生异常---异常信息：\n"+e.getMessage());
			response = new GetOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());
			ebayRecursive(0);
		}
	}

	public void amazonRecursive(int a) {
		try {
			if (a > 0) {
				response = amazonService.getOrder(request);
			}
			// 只有请求成功或请求次数上限才为true
			flag = true;
		} catch (MarketplaceWebServiceOrdersException e) {
			response = new GetOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());
			--a;
			if(a==0){//最后一次调用失败后记录日志 
				LOGGER.error("【amazon】--调用获取订单业务类时发生Marketplace异常---异常信息：\n"+e.getMessage());
			}
			amazonRecursive(a);//可能有网络原因所以多调用几次
		} catch (CheckDataException e) {
			LOGGER.error("【amazon】--调用获取订单业务类时发生CheckData异常---异常信息：\n"+e.getMessage());
			response = new GetOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());
			amazonRecursive(0);
		} catch (Exception e) {
			LOGGER.error("【amazon】--调用获取订单业务类时发生异常---异常信息：\n"+e.getMessage());
			response = new GetOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());
			amazonRecursive(0);
		}
	}

	public void smtRecursive(int a) {
		try {
			if (a > 0) {
				response = smtService.getOrder(request);
			}
			flag = true;
		}catch(HttpException e){
			response = new GetOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());
			--a;
			if(a==0){//最后一次调用失败后记录日志 
				LOGGER.error("【smt】--调用获取订单业务类时发生Http请求异常---异常信息：\n"+e.getMessage());	
			}
			smtRecursive(a);
		} catch (CheckDataException e) {
			LOGGER.error("【smt】--调用获取订单业务类时发生CheckData异常---异常信息：\n"+e.getMessage());
			response = new GetOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());
			smtRecursive(0);
		} catch (IOException e) {
			LOGGER.error("【smt】--调用获取订单业务类时发生IO异常---异常信息：\n"+e.getMessage());
			response = new GetOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());
			smtRecursive(0);
		} catch(Exception e){
			LOGGER.error("【smt】--调用获取订单业务类时发生异常---异常信息：\n"+e.getMessage());
			response = new GetOrderResponse();
			response.setAck(Constant.ACK_FAIL);
			response.setFailMessage(e.getMessage());
			smtRecursive(0);
		}
	}
	/**
	 * 超时时间 5*12=60秒
	 * 
	 * @createtime 2015年1月10日 上午9:33:12
	 * @author jonsen
	 */
	public GetOrderResponse getResponse() {
		int i = 0;
		while (i < 36) {// 等待三分钟
			if (this.response == null || !flag) {
				try {
					Thread.sleep(5000);
					// i=0;//重置等待时间
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} else {
				break;
			}
			i++;
		}
		return response;
	}
}
